package com.rcloud.server.sealtalk.xiangyun;

import com.rcloud.server.sealtalk.constant.ConstantsURL;
import com.rcloud.server.sealtalk.util.Base64Util;
import com.rcloud.server.sealtalk.util.MultipartFileToFile;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class NetOCRCommonClient {
    @Value("${sealtalk-config.ocr_key}")
    private String ocrKey;

    @Value("${sealtalk-config.ocr_secret}")
    private String ocrSecret;


    /**
     * 证件识别
     *
     * @throws IOException
     */
    public String identifyCert(MultipartFile multipartFile, String regType) throws IOException {
        //先转换成流文件
        File file = MultipartFileToFile.multipartFileToFile(multipartFile);
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file))
                .addFormDataPart("key", ocrKey)
                .addFormDataPart("secret", ocrSecret)
                //识别类型（二代证2；行驶证6；驾照5；其他详见附录）
                .addFormDataPart("typeId", regType)
                .addFormDataPart("format", "json")
                .build();
        Request request = new Request.Builder()
                .url(ConstantsURL.recog)//需修改为对应产品接口url
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        String string = response.body().string();
        log.info("证件识别返回信息. response:{}", string);
        return string;
    }

    /**
     * 营业执照识别
     */
    public String identifyBusinessLicense(File file) {
        //先转换成流文件
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(20, TimeUnit.SECONDS)//设置读取超时时间
                .build();
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file))
                .addFormDataPart("key", ocrKey)
                .addFormDataPart("secret", ocrSecret)
                .addFormDataPart("typeId", "2008")
                .addFormDataPart("outvalue", "0")
                .addFormDataPart("format", "json")
                .build();
        Request request = new Request.Builder()
                .url(ConstantsURL.recoglen)//需修改为对应产品接口url
                .post(body)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String string = response.body().string();
            log.info("营业执照识别返回信息. response:{}", string);
            return string;
        } catch (IOException e) {
            e.printStackTrace();
            log.info("营业执照识别错误信息. error_msg:{}", e.getMessage());
            return "";
        }
    }

    /**
     * 企业工商信息查询
     */
    public String queryBusinessInfo(String entName) throws IOException {
        //先转换成流文件
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("key", ocrKey)
                .addFormDataPart("secret", ocrSecret)
                //企业或个体名称（utf-8格式base64加密）
                .addFormDataPart("entName", entName)
                .addFormDataPart("typeId", "3016")
                .addFormDataPart("format", "json")
                .build();
        Request request = new Request.Builder()
                .url(ConstantsURL.verLicenceverLicencePlus)//需修改为对应产品接口url
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            log.info("企业工商信息查询返回信息. response:{}", string);
            return string;
        } catch (IOException e) {
            e.printStackTrace();
            log.info("企业工商信息查询错误信息. error_msg:{}", e.getMessage());
            return "";
        }
    }
    /**
     * 人脸识别
     */
    public String identifyFace(MultipartFile imgFile1, MultipartFile imgFile12) throws IOException {
        File file1 = MultipartFileToFile.multipartFileToFile(imgFile1);
        File file2 = MultipartFileToFile.multipartFileToFile(imgFile12);
        //先转换成流文件
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("img1", file1.getName(), RequestBody.create(MediaType.parse("image/png"), file1))
                .addFormDataPart("img1", file2.getName(), RequestBody.create(MediaType.parse("image/png"), file2))
                .addFormDataPart("key", ocrKey)
                .addFormDataPart("secret", ocrSecret)
                .addFormDataPart("typeId", "21")
                .addFormDataPart("format", "json")
                .build();
        Request request = new Request.Builder()
                .url("URL人脸识别")//需修改为对应产品接口url
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        String string = response.body().string();
        log.info("企业工商信息查询返回信息. response:{}", string);
        return string;
    }

    /**
     * 身份证号实名认证-增强版
     *
     * @throws IOException
     */
    public String realNameStrengthen(String trueName, String idenNo) throws IOException {
        OkHttpClient client = new OkHttpClient();
        RequestBody body = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("key", ocrKey)
                .addFormDataPart("secret", ocrSecret)
                .addFormDataPart("trueName", trueName)
                .addFormDataPart("idenNo", idenNo)
                .addFormDataPart("typeId", "3001")//需修改为对应产品ID
                .addFormDataPart("format", "json")
                .build();
        Request request = new Request.Builder()
                .url(ConstantsURL.veridenNo)//需修改为对应产品接口url
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        String string = response.body().string();
        log.info("身份证号实名认证返回信息. response:{}", string);
        return string;
    }


}
